﻿using Bangle.BLL;
using Bangle.Common;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Account_UserList : BasePage
{
    #region 属性
    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? "" : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }
    #endregion

    Sys_RoleBLL bll = new Sys_RoleBLL();
    Sys_UserBLL user_bll = new Sys_UserBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        setUserControlStatus();

        if (!IsPostBack)
        {
            //设置分页控件文本
            setLanguage();

            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    /// <summary>
    /// 设置分页控件文本
    /// </summary>
    private void setLanguage()
    {
        TextUtility.SetPaginateCtlText(PaginateCtl);
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("001003");
        this.btn_Add.Enabled = base.CheckUserPermissionOperation("001003", "Add");
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        PaginateCtl.PageSize = 10;
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;

        UtilityBLL ubll = new UtilityBLL();

        int rCount = 0;
        DataTable dt = ubll.GetListByPage("Sys_User", "*", "UserId", swhere, 10, 1, 0, out rCount);

        //绑定数据源
        RepeaterList.DataSource = dt;
        //返回记录条数
        PaginateCtl.RecordCount = rCount;

        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        RepeaterList.DataBind();
    }
    #endregion

    #region 查询
    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string swhere = "1=1";
        if (this.txt_UserName.Text.Trim().Length > 0)
        {
            swhere += " and UserName like '%" + TextUtility.ReplaceText(this.txt_UserName.Text.Trim()) + "%'";
        }
        if (this.txt_Account.Text.Trim().Length > 0)
        {
            swhere += " and Account like '%" + TextUtility.ReplaceText(this.txt_Account.Text.Trim()) + "%'";
        }
        StrWhere = swhere;
        //分页控件回到第1页
        PaginateCtl.CurrentPageIndex = 1;
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbl_Valid = (Label)e.Item.FindControl("lbl_Valid");
            LinkButton lbtn_Valid = (LinkButton)e.Item.FindControl("lbtn_Valid");

            lbtn_Valid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                lbl_Valid.Text = "禁用";
                lbl_Valid.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lbl_Valid.Text = "启用";
                lbl_Valid.ForeColor = System.Drawing.Color.Green;
            }

            #region 用户权限
            LinkButton lbtn_Detail = (LinkButton)e.Item.FindControl("lbtn_Detail");
            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Password = (LinkButton)e.Item.FindControl("lbtn_Password");


            lbtn_Detail.Visible = base.CheckUserPermissionOperation("001003", "View");
            lbtn_Modify.Visible = base.CheckUserPermissionOperation("001003", "Edit");
            lbtn_Valid.Visible = base.CheckUserPermissionOperation("001003", "Valid");
            lbtn_Password.Visible = base.CheckUserPermissionOperation("001003", "Edit");
            #endregion
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        switch (e.CommandName)
        {
            case "Valid":
                {
                    try
                    {
                        Sys_User model = new Sys_User();
                        model = user_bll.GetModel(id);
                        model.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        model.UpdateUserId = base.CurrentSession.UserID;
                        model.Valid = model.Valid == 0 ? 1 : 0;
                        user_bll.Update(model);

                        BindRepeaterData(StrWhere);
                    }
                    catch (Exception ex)
                    {
                        base.AlertError(ex.ToString());
                    }
                    break;
                }

            case "Modify":
                {
                    Response.Redirect("UserEdit.aspx?Action=Modify&Id=" + id.ToString());
                    break;
                }
            case "Detail":
                {
                    Response.Redirect("UserEdit.aspx?Action=Detail&Id=" + id.ToString());
                    break;
                }
            case "Password":
                {
                    Sys_User model = user_bll.GetModel(id);
                    if (model == null)
                    {
                        base.Alert("操作失败！");
                        return;
                    }
                    model.UserPwd = MD5.EncryptMD5("123456");
                    try
                    {
                        user_bll.Update(model);
                    }
                    catch (Exception ex)
                    {
                        base.AlertError(ex.ToString());
                    }
                    base.Alert("该用户密码重置成功！新密码为：123456");
                    break;
                }
        }
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData(StrWhere);
    }
    #endregion
}